package com.liukai.bishe.ui.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.model.doman.transaction.TransactionTotal;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionPresenter;
import com.liukai.bishe.ui.activity.MainActivity;
import com.liukai.bishe.ui.activity.RecordActivity;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.DateUtils;
import com.robinhood.ticker.TickerUtils;
import com.robinhood.ticker.TickerView;

import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.appcompat.widget.AppCompatButton;

/**
 * 查看事务总的执行次数，以及总的执行时间
 */
public class TransactionInfoDialog{

    private Context mContext;
    private Transaction mTransaction;
    private TransactionTotal mTransactionTotal;

    private AlertDialog mDialog = null;
    private final View mView_dialog;

    private TextView mTvTitle;
    private TickerView mTvTotalCount;
    private TickerView mTvTotalTime;
    private AppCompatButton mBtnEdit;
    private AppCompatButton mBtnDelete;
    private AppCompatButton mBtnRecord;

    private ITransactionPresenter mITransactionPresenter;


    public TransactionInfoDialog(Context context, Transaction transaction, TransactionTotal transactionTotal, ITransactionPresenter iTransactionPresenter){

        this.mContext = context;
        this.mTransaction = transaction;
        this.mTransactionTotal = transactionTotal;
        this.mITransactionPresenter = iTransactionPresenter;


        mView_dialog = View.inflate(context,R.layout.dialog_transaction_info,null);

        if (mDialog == null){
            // 创建AlertDialog对象
            mDialog = new AlertDialog.Builder(mContext).create();
            mDialog.show();
            // 设置点击可取消
            mDialog.setCancelable(true);
            // 获取Window对象
            Window window = mDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            // 设置显示视图内容
            window.setContentView(mView_dialog);

//            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
//            lp.width = 500;// 调整该值可以设置对话框显示的宽度
//            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//            window.setAttributes(lp);
        }else {
            mDialog.show();
        }

        initView();
        initListener();


    }

    private void initListener() {


        // 编辑按钮
        mBtnEdit.setOnClickListener(view -> {
            AddTranscationDialog dialog = new AddTranscationDialog(mContext,mITransactionPresenter,mTransaction);
            dialog.show();
            mDialog.dismiss();
        });

        // 删除按钮
        mBtnDelete.setOnClickListener(view -> {

            String id = mTransaction.getId();
            mITransactionPresenter.deleteTransactionById(id,Constants.getUserInfo().getUsername());
            mDialog.dismiss();
        });

        // 历史记录按钮
        mBtnRecord.setOnClickListener(view -> {


            TimePickerView pvTime = new TimePickerBuilder(mContext, (date, v) -> {
//                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Toast.makeText(mContext, date.getTime()+"", Toast.LENGTH_SHORT).show();
                long timeMills = date.getTime();
                Intent intent = new Intent(mContext,RecordActivity.class);
                intent.putExtra("timeMills",String.valueOf(timeMills));
                mContext.startActivity(intent);
            }).setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                    .setCancelText("取消")//取消按钮文字
                    .setSubmitText("确定")//确认按钮文字
                    .setTitleSize(20)//标题文字大小
                    .setTitleText("Title")//标题文字
                    .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                    .isCyclic(true)//是否循环滚动
                    .setTitleColor(mContext.getColor(R.color.white))//标题文字颜色
                    .setSubmitColor(mContext.getColor(R.color.white))//确定按钮文字颜色
                    .setCancelColor(mContext.getColor(R.color.white))//取消按钮文字颜色
                    .setTitleBgColor(mContext.getColor(R.color.colorPrimary))//标题背景颜色 Night mode
                    .setBgColor(mContext.getColor(R.color.white))//滚轮背景颜色 Night mode
                    .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                    .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .isDialog(true)//是否显示为对话框样式
                    .setTextColorCenter(mContext.getColor(R.color.colorPrimary))
                    .setTextColorOut(mContext.getColor(R.color.colorNoSexGrey))
                    .build();
            pvTime.show();
            mDialog.dismiss();

        });



    }

    private void initView() {

        mTvTitle = mDialog.findViewById(R.id.tvTitle); // 标题
        mTvTotalCount = mDialog.findViewById(R.id.tvTotalCount); // 总执行次数
        mTvTotalTime = mDialog.findViewById(R.id.tvToTalTime); // 总执行时间
        mBtnEdit = mDialog.findViewById(R.id.btnEdit);
        mBtnDelete = mDialog.findViewById(R.id.btnDelete);
        mBtnRecord = mDialog.findViewById(R.id.btnRecord);

        mTvTotalCount.setCharacterLists(TickerUtils.provideNumberList());
        mTvTotalTime.setCharacterLists(TickerUtils.provideNumberList());

        mTvTitle.setText(mTransaction.getTransactionname());
        String totalCount = mTransactionTotal.getCount();
        String totalTime = mTransactionTotal.getTotaltime();

        if (totalCount != null) {
            mTvTotalCount.setText(mTransactionTotal.getCount());
        }else{
            mTvTotalCount.setText("0");
        }
        if (totalTime != null) {
            String totalTimeMinuties = DateUtils.longTimeToMinuties(Long.valueOf(totalTime));
            mTvTotalTime.setText(totalTimeMinuties);
        }else{
            mTvTotalTime.setText("0");
        }



    }

    public void show(){
        mDialog.setCanceledOnTouchOutside(true);
//        mDialog.show();
    }


//    public void setHandler(Handler handler) {
//        this.mHandler = handler;
//    }
}
